//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VanillaOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CVanillaOption::typeid)
{
    CStrikedTypePayoff^ _Cpayoff;
    CExercise^ _Cexercise;
    try
    {
#ifdef HANDLE
        _phVanillaOption = NULL;
#endif
        _Cpayoff = safe_cast<CStrikedTypePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::StrikedTypePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::StrikedTypePayoff>&> (_Cpayoff->GetShared ()); 
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        _ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (new QuantLib::VanillaOption ( _payoff,  _exercise ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppVanillaOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cexercise != nullptr) _Cexercise->Unlock();
    }
}
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (boost::shared_ptr<QuantLib::VanillaOption>& childNative, Object^ owner) : COneAssetOption(CVanillaOption::typeid)
{
#ifdef HANDLE
	_phVanillaOption = NULL;
#endif
	_ppVanillaOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
}
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (QuantLib::VanillaOption& childNative, Object^ owner) : COneAssetOption(CVanillaOption::typeid)
{
#ifdef HANDLE
	_phVanillaOption = NULL;
#endif
	_ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
    _VanillaOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Instruments::CVanillaOption::CVanillaOption (CVanillaOption^ copy) : COneAssetOption(CVanillaOption::typeid)
{
#ifdef HANDLE
	_phVanillaOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
    }
}
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (PLATFORM::Type^ t) : COneAssetOption(CVanillaOption::typeid)
{
#ifdef HANDLE
	_phVanillaOption = NULL;
#endif
	if (!t->IsSubclassOf(CVanillaOption::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (QuantLib::Handle<QuantLib::VanillaOption>& childNative, Object^ owner)  : COneAssetOption(CVanillaOption::typeid)
{
	_phVanillaOption = &childNative;
	_ppVanillaOption = &static_cast<boost::shared_ptr<QuantLib::VanillaOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
    _VanillaOptionOwner = owner;
}
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (QuantLib::Handle<QuantLib::VanillaOption> childNative)  : COneAssetOption(CVanillaOption::typeid)
{
	_phVanillaOption = &childNative;
	_ppVanillaOption = &static_cast<boost::shared_ptr<QuantLib::VanillaOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CVanillaOption::CVanillaOption (QuantLib::VanillaOption childNative)  : COneAssetOption(CVanillaOption::typeid)
{
#ifdef HANDLE
	_phVanillaOption = NULL;
#endif
	_ppVanillaOption = new boost::shared_ptr<QuantLib::VanillaOption> (new QuantLib::VanillaOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppVanillaOption));
}
#endif

Cephei::QL::Instruments::CVanillaOption::~CVanillaOption ()
{
    if (_ppVanillaOption != NULL)
    {
	    delete _ppVanillaOption;
        _ppVanillaOption = NULL;
    }
}
Cephei::QL::Instruments::CVanillaOption::!CVanillaOption ()
{
    if (_ppVanillaOption != NULL)
    {
	    delete _ppVanillaOption;
    }
}
QuantLib::VanillaOption& Cephei::QL::Instruments::CVanillaOption::GetReference ()
{
    if (_ppVanillaOption == NULL) throw REFNEW NativeNullException ();
	return **_ppVanillaOption;
}
boost::shared_ptr<QuantLib::VanillaOption>& Cephei::QL::Instruments::CVanillaOption::GetShared ()
{
    if (_ppVanillaOption == NULL) throw REFNEW NativeNullException ();
	return *_ppVanillaOption;
}
QuantLib::VanillaOption* Cephei::QL::Instruments::CVanillaOption::GetPointer ()
{
    if (_ppVanillaOption == NULL) throw REFNEW NativeNullException ();
	return &**_ppVanillaOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VanillaOption>& Cephei::QL::Instruments::CVanillaOption::GetHandle ()
{
	if (_phVanillaOption == NULL)
	{
		_phVanillaOption = new Handle<QuantLib::VanillaOption> (*_ppVanillaOption);
	}
	return *_phVanillaOption;
}
#endif
bool Cephei::QL::Instruments::CVanillaOption::HasNative () 
{
	return (_ppVanillaOption != NULL);
}

Double Cephei::QL::Instruments::CVanillaOption::ImpliedVolatility (Double price, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations, Microsoft::FSharp::Core::FSharpOption<Double>^ minVol, Microsoft::FSharp::Core::FSharpOption<Double>^ maxVol)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
        QuantLib::Real _price = (QuantLib::Real)ValueHelper::Convert (price); //a
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-4); //9a
        QuantLib::Size _maxEvaluations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxEvaluations) ? (QuantLib::Size)ValueHelper::Convert (maxEvaluations->Value) : 100); //9a
        QuantLib::Volatility _minVol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (minVol) ? (QuantLib::Volatility)ValueHelper::Convert (minVol->Value) : 1.0e-7); //9a
        QuantLib::Volatility _maxVol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (maxVol) ? (QuantLib::Volatility)ValueHelper::Convert (maxVol->Value) : 4.0); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppVanillaOption)->impliedVolatility ( _price,  _process,  _accuracy,  _maxEvaluations,  _minVol,  _maxVol );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IVanillaOption^ Cephei::QL::Instruments::CVanillaOption_Factory::Create (Cephei::QL::Instruments::IStrikedTypePayoff^ payoff, Cephei::QL::IExercise^ exercise, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CVanillaOption ( payoff,  exercise,  QL_Pricer);
}
